#!/usr/bin/perl

###########################################################################################################################################################################################
#                                                                                                                                  
#	 edit-nsclientPP-remote.pl v1 by Jamie Geyer jgeyer14-at-gmail-d0t-c0m -- 5/27/2011 
#   edit windows nsclient++ configurations remotely.
# 	must be able to edit regular expressions directly to use this, see commands section below.
#
###########################################################################################################################################################################################
# This script may be used and redistributed freely, under the following conditions:

#	1. Do not alter any information above this line, unless you alter the script.
#	2. If you improve upon this script, please share! Post a comment in the original post, or email the improvement.  
#	   I will give you credit and put a link to your site in the blog post.

###########################################################################################################################################################################################

# Copyright [2011] [Jamie Geyer]

#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at

#       http://www.apache.org/licenses/LICENSE-2.0

#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.


###########################################################################################################################################################################################

# YOU HAVE TO EDIT STUFF IN THIS SECTION !!! #

	# enter each host seperated by a space
	@hosts = qw(192.168.1.3);

	# edit this variable to the install path for nsclient++ on the remote server.
	$remoteNscppPath = "c:\\program files\\nsclient++";
	
	# if you want to restart the nsclient++ service remotely, you need psexec from sysinternals - it's free.
	$localSysinternalsPath = "C:\\some\\path\\to\\SysinternalsSuite";
	print $localSysinternalsPath;
	
	# change these to whatever before using
	
	# you may have to use domain\\user
	
	$user = "domain\\user"; 
	$pass = "password";


# END #

###############################################


if ($ARGV[0] !~ m/^yes$|^no$/i){

	print "\n\nUSAGE:  perl editRemoteConfig.pl <(yes|no)>  -- provide yes or no to reload service after copying config\n\n" ;
	exit 2;
}

$restart = $ARGV[0];

if ($remoteNscppPath =~ /(\w):\\(.+)/){

	$remoteInstallDrive = $1;
	$remoteProgramPath = $2;
	# get rid of trailing back-slash in path, if it exists - for predictability.
	#$remoteProgramPath =~ s/\\\\$//;
	# get rid of leading back-slash in path, for predictibility.
	#$remoteProgramPath =~ s/^\\\\//;
	
	# finally, change single backslashes to double to escape for shell command
	
	#$remoteProgramPath =~ s/\\/\\\\/g;
	
} else {

	print "The remote nsclient++ variable does not appear to be correct.  Please check and try again.\n\n";
	print "You provided $remoteNscppPath\n\n";
	exit 2;

}


# EXAMPLES #########################################################################################################################
####################################################################################################################################
# ADD NEW CHECK HANDLER:   $_ =~ s/(\;check_vbs\=cscript.exe \/\/T:30 \/\/NoLogo scripts\\check_vb\.vbs)/$1\nsample_check=test-1/; #
# EDIT allowed_hosts:   $_ =~ s/^allowed_hosts.+/allowed_hosts=10.52.149.126,10.52.149.127,24.40.73.80,24.40.73.81,68.87.97.254/g;  #
####################################################################################################################################




# iterate through each host and do the replace.

$currentTimeStamp = get_timestamp();

foreach $host (@hosts) {

	$currentHost = $host;
	#$currentTimeStamp = time;
	
	chomp $currentTimeStamp;
	$currentFileName = "NSC.ini-".$currentHost."-".$currentTimeStamp;

	`net use "\\\\$currentHost\\$remoteInstallDrive\$\\$remoteProgramPath" /user:$user $pass`;
	
	print "\\\\$currentHost\\$remoteInstallDrive\$\\$remoteProgramPath\n";

	# step 1. make a backup on the remote server

	print "Backing up config on local server $currentHost...\n";
	`copy "\\\\$currentHost\\$remoteInstallDrive\$\\$remoteProgramPath\\NSC.ini" "\\\\$currentHost\\$remoteInstallDrive\$\\$remoteProgramPath\\$currentFileName"`;

	# step 2 copy file down locally as NSC.ini-<server_IP_or_host>-<timestamp>
	print "Copying config down for editing on $currentHost ...\n";
	`copy "\\\\$currentHost\\$remoteInstallDrive\$\\$remoteProgramPath\\NSC.ini" $currentFileName`;

	# step 3 - open file and make change(s)

	open thefile, $currentFileName;
	open theNewFile, ">NSC.ini.txt";

	print "Making local edits...\n";

	while (<thefile>){
	
	##################################################################
	# ************** ADD COMMANDS HERE BELOW CHOMP ***************** #
	##################################################################
	
		chomp;
		
		# editing the 'allowed_hosts' paramater is easy! just capture the existing host entry with a regex
		# and replace it with itself, plus the new hosts! simple!
		
		$_ =~ s/^allowed_hosts\=(.+)/allowed_hosts=$1,10.10.10.10/;
		
		
		##########################################################################################################################################################
		
		
		# A good way to add a new handler is find one that exists already, and append a newline along with the new check information - as demonstrated below.
		
		# ADD NEW CHECK HANDLER:   $_ =~ s/(\;check_vbs\=cscript.exe \/\/T:30 \/\/NoLogo scripts\\check_vb\.vbs)/$1\nsample_check=cscript.exe \/\/nologo scripts\\someCheck.vbs args etc/; #
		
		##########################################################################################################################################################
		
		### ******* use this command to alter some command ****** ###
		
		#$_ =~ s/check_something.+/check_something\=cscript.exe \/\/nologo scripts\\checkSomething.vbs some args/;
	
		
	
		print theNewFile $_."\n";

	}

	close thefile;
	close theNewFile;

	# step 4, copy new file up
	print "copying new file to  $currentHost ...";
	`copy NSC.ini.txt "\\\\$currentHost\\$remoteInstallDrive\$\\$remoteProgramPath\\NSC.ini"`;

	# restart service to pick up new config if specified by command line to do so
	
	if ($restart =~ /yes/i){

		
	
		print "stopping services on $currentHost...\n";
		print "$localSysinternalsPath\\psexec \\\\$currentHost  net stop nsclientpp\n";
		`$localSysinternalsPath\\psexec \\\\$currentHost  net stop nsclientpp 2>> sysInternals-$currentTimeStamp.out`;

		print "starting services on $currentHost...\n";
		`$localSysinternalsPath\\psexec \\\\$currentHost  net start nsclientpp 2>> sysInternals-$currentTimeStamp.out`;
		
		}

		# last step delete the connection
		`net use "\\\\$currentHost\\$remoteInstallDrive\$\\$remoteProgramPath" /d`;
		
	

}


########## generate unique timestamp for backup file name #######


sub get_timestamp {

   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
   if ($mon < 10) { $mon = "0$mon"; }
   if ($hour < 10) { $hour = "0$hour"; }
   if ($min < 10) { $min = "0$min"; }
   if ($sec < 10) { $sec = "0$sec"; }
   $year=$year+1900;

   return $year . '_' . $mon . '_' . $mday . '__' . $hour . '_' . $min . '_' . $sec;
}







